<?php
/**
 * Created by PhpStorm.
 * User: huqi
 * Date: 2017/3/23
 * Time: 20:55
 */

namespace App\Http\Controllers\ticai;
use App\Services\Ticai;
use Illuminate\Http\Request;
use Cache ;
include __DIR__.'/../../../../vendor/phpQuery/phpQuery.php' ;
class IndexController
{
    const CACHE_TIME = 1;
    const CACHE_OLD = 5 ;
    public function actionIndex(){
        $data = $this->getCachedData('page_index',self::CACHE_TIME , function (){
            \phpQuery::newDocumentFile('http://m.gooooal.com/lotwap/sh/lot/data/bobao') ;
            $lis = [] ;
            foreach (pq('.CPtable02') as  $t){
                $content = pq($t)->htmlOuter() ;
                $tag =  $this->findTag($content) ;
                if($tag == 'sh115'){
                    //continue;
                }
                $lis[] = ['con' => $content , 'tag' => $tag , 'url' => Ticai::getUrl($tag , ['tag'=>$tag])] ;
            }
            return $lis ;
        });
        return view('index',['data' => $data]) ;
    }

    public function actionGroup(){
        $tag = $this->getReq()->get('tag') ;
        $d = $this->getReq()->get('d','') ;
        $config = config("ticai.{$tag}",'');
        if(!empty($config) ){
            $data = $this->getPageData('group',function () use($config,$d){
                $api = $config['api'] . "/{$d}" ;
                \phpQuery::newDocumentFile($api) ;
                $rs = [] ;
                $rs['ta'] = pq('.CPtableA')->htmlOuter() ;
                $rs['tb'] = pq('.CPtableB')->htmlOuter() ;
                $rs['tc'] = pq('.CPtableC')->htmlOuter() ;
                $rs['title'] = pq('.Stitle')->htmlOuter() ;
                return $rs ;
            });
            return view('group' , ['data' => $data]);
        }else{
            exit('页面不存在') ;
        }
    }

    public function actionGroupB(){
        $tag = $this->getReq()->get('tag') ;
        $d = $this->getReq()->get('d','') ;
        $config = config("ticai.{$tag}",'');
        if(!empty($config) ){
            $data = $this->getPageData('gorupB',function ()use($config,$d){
                $api = $config['api'] . "/{$d}" ;
                \phpQuery::newDocumentFile($api) ;
                $rs = [] ;
                $rs['ta'] = pq('.CPtableA')->htmlOuter() ;
                $rs['tb'] = pq('.CPtableB')->htmlOuter() ;
                $rs['tc'] = pq('.CPtableC')->htmlOuter() ;
                $rs['title'] = pq('.Stitle')->htmlOuter() ;
                return $rs ;
            });
            return view('groupB' , ['data' => $data]);
        }else{
            exit('页面不存在') ;
        }
    }

    public function actionGroupC(){
        $tag = $this->getReq()->get('tag') ;
        $d = $this->getReq()->get('d','') ;
        $config = config("ticai.{$tag}",'');
        if(!empty($config) ){
            $api = $config['api'] . "/{$d}" ;
            $data = $this->getPageData('groupC',function ()use($api){
                \phpQuery::newDocumentFile($api) ;
                $rs = [] ;
                $rs['te'] = pq('.CPtableE')->htmlOuter() ;
                $rs['title'] = pq('.Stitle')->htmlOuter() ;
                return $rs ;
            }) ;
            return view('groupC' , ['data' => $data]);
        }else {
            exit('页面不存在');
        }
    }

    public function actionGroupD(){
        $tag = $this->getReq()->get('tag') ;
        $d = $this->getReq()->get('d','') ;
        // $key = 'group'.md5($tag.$time)
        $config = config("ticai.{$tag}",'');
        if(!empty($config) ){
            $api = $config['api'] . "/{$d}" ;
            $data = $this->getPageData('groupD',function ()use($api){
                \phpQuery::newDocumentFile($api) ;
                $rs = [] ;
                $rs['tb'] = pq('.CPtableB')->htmlOuter() ;
                $rs['title'] = pq('.Stitle')->htmlOuter() ;
                return $rs ;
            });
            return view('groupD' , ['data' => $data]);
        }else {
            exit('页面不存在');
        }
    }
    private  function findTag($content){
        $config = config('ticai') ;
        foreach ($config as $k => $v){
            if(strpos($content , $v['name']) !== false ){
                return $k ;
            }
        }
        return '' ;
    }

    /**
     * @return Request
     */
    private function getReq(){
        return app('request') ;
    }

    public function getPageData($action,$func){
        $tag = $this->getReq()->get('tag') ;
        $d = $this->getReq()->get('d','') ;
        if($d){
            $ttl = self::CACHE_OLD ;
            $key = "{$action}_old_".md5($tag.$d) ;
        }else{
            $ttl = self::CACHE_TIME ;
            $key = "{$action}_group_page_index_".$tag ;
        }
        $data = $this->getCachedData($key,$ttl,function () use($func){
            return $func() ;
        });
        return $data ;
    }

    public function getCachedData($key,$ttl,$func){
        if(Cache::has($key)){
            return Cache::get($key) ;
        }
        try{
            $data = $func() ;
            if(!empty($data)){
                Cache::add($key ,$data , $ttl) ;
            }
            return $data ;
        }catch (\Exception $e){
            exit('网络错误') ;
        }
    }
}